import java.util.Date;
import java.util.Set;

/**
 * @program: 程序设计理论与方法_202130504106_李航
 * @description: 有5个具有5种不同颜色的房间；
 * 每个房间里分别住着一个不同国籍的人；
 * 每个人都在喝一种特定品牌的饮料；
 * 抽一特定品牌的香烟；
 * 养一特定的宠物；
 * 没有任意的两个人在抽相同品牌的烟、或喝相同品牌饮料、或养相同的宠物。
 * 同时又知：
 * 1.英国人住在红房子里；
 * 2.瑞典人养狗；
 * 3.丹麦人喝茶；
 * 4.绿房子紧挨着白房子，在白房子的左边；
 * 5.绿房子主人喝咖啡；
 * 6.抽PALL MALL牌香烟的人养鸟；
 * 7.黄房子里的人抽DUNHILL牌的烟；
 * 8.住中间房子的人喝牛奶；
 * 9.挪威人住在第一个房子里（最左边）；
 * 10.抽BLENDS香烟的人和养猫的人相邻；
 * 11.养马的人和抽DUNHILL牌香烟的人相邻；
 * 12.抽BLUEMASTER牌香烟的人喝啤酒；
 * 13.德国人抽PRINCE牌香烟；
 * 14.挪威人和住蓝房子的人相邻；
 * 15.抽BLENDS牌香烟的人和喝矿泉水的人相邻。
 * 问：谁在养鱼？
 * @author: LiHang
 * @create: 2021-09-13 18:05
 **/

public class Einstein {
    public static void main(String[] args) {
        Einstein einstein = new Einstein();

        //筛选版-暴力破解
        long startTime = System.currentTimeMillis();
        String resultSingleCheck = einstein.baoliSingleCheck();
        System.out.println(resultSingleCheck);
        long endTime = System.currentTimeMillis();
        System.out.println("筛选版程序运行时间：" + (endTime - startTime) + "ms");

        //非筛选版-暴力破解
        startTime = System.currentTimeMillis();
        String resultSingleNoCheck = einstein.baoliSingleNoCheck();
        System.out.println(resultSingleNoCheck);
        endTime = System.currentTimeMillis();
        System.out.println("筛选版程序运行时间：" + (endTime - startTime) + "ms");


    }

    /**
     * 暴力破解法，无限for循环
     * 做到一半想到可以用二维数组= -、
     * 这个先写完，然后再写个二维数组的版本
     * 要修改顺序，加快迭代，不然真的太慢了= -、
     * 所以写两个版本，一个筛选，一个不筛选，这个就是筛选的
     * 原来想着条件要放在越前面筛选越好，结果直接跳过了，所以要放在相对的后面
     *
     * @return
     */
    public String baoliSingleCheck() {
        int[] country = {1, 1, 1, 1, 1};  //分别代表英国，瑞典，丹麦，挪威，德国
        int[] house = {1, 1, 1, 1, 1}; //红房子，白房子，绿房子，蓝房子，黄房子
        int[] drink = {1, 1, 1, 1, 1}; //喝茶,喝咖啡,喝牛奶,喝啤酒，喝矿泉水
        int[] smoke = {1, 1, 1, 1, 1}; //PALL MALL牌香烟,DUNHILL牌的烟,BLENDS香烟,BLUEMASTER牌香烟，PRINCE牌香烟
        int[] pets = {1, 1, 1, 1, 1}; //狗,鸟,猫,马,鱼
        String result = "";

        for (country[0] = 1; country[0] <= 5; country[0]++) {
            for (country[1] = 1; country[1] <= 5; country[1]++) {
                for (country[2] = 1; country[2] <= 5; country[2]++) {
                    for (country[3] = 1; country[3] <= 5; country[3]++) {
                        for (country[4] = 1; country[4] <= 5; country[4]++) {
                            if (isRepeatNum(country)) {
                                continue;
                            }
                            //9.挪威人住在第一个房子里（最左边）；
                            if (country[3] == 1) {

                                for (house[0] = 1; house[0] <= 5; house[0]++) {
                                    for (house[1] = 1; house[1] <= 5; house[1]++) {
                                        for (house[2] = 1; house[2] <= 5; house[2]++) {
                                            for (house[3] = 1; house[3] <= 5; house[3]++) {
                                                for (house[4] = 1; house[4] <= 5; house[4]++) {
                                                    if (isRepeatNum(house)) {
                                                        continue;
                                                    }
                                                    //1.英国人住在红房子里；
                                                    if (country[0] == house[0]) {
                                                        //4.绿房子紧挨着白房子，在白房子的左边；
                                                        if (house[2] == (house[1] - 1)) {
                                                            //14.挪威人和住蓝房子的人相邻；
                                                            if (country[3] == (house[3] + 1) || country[3] == (house[3] - 1)) {

                                                                for (drink[0] = 1; drink[0] <= 5; drink[0]++) {
                                                                    for (drink[1] = 1; drink[1] <= 5; drink[1]++) {
                                                                        for (drink[2] = 1; drink[2] <= 5; drink[2]++) {
                                                                            for (drink[3] = 1; drink[3] <= 5; drink[3]++) {
                                                                                for (drink[4] = 1; drink[4] <= 5; drink[4]++) {
                                                                                    if (isRepeatNum(drink)) {
                                                                                        continue;
                                                                                    }
                                                                                    //3.丹麦人喝茶；
                                                                                    if (country[2] == drink[0]) {
                                                                                        //5.绿房子主人喝咖啡；
                                                                                        if (house[2] == drink[1]) {
                                                                                            //8.住中间房子的人喝牛奶；
                                                                                            if (drink[2] == 3) {

                                                                                                for (smoke[0] = 1; smoke[0] <= 5; smoke[0]++) {
                                                                                                    for (smoke[1] = 1; smoke[1] <= 5; smoke[1]++) {
                                                                                                        for (smoke[2] = 1; smoke[2] <= 5; smoke[2]++) {
                                                                                                            for (smoke[3] = 1; smoke[3] <= 5; smoke[3]++) {
                                                                                                                for (smoke[4] = 1; smoke[4] <= 5; smoke[4]++) {
                                                                                                                    if (isRepeatNum(smoke)) {
                                                                                                                        continue;
                                                                                                                    }
                                                                                                                    //7.黄房子里的人抽DUNHILL牌的烟；
                                                                                                                    if (house[4] == smoke[1]) {
                                                                                                                        //12.抽BLUEMASTER牌香烟的人喝啤酒；
                                                                                                                        if (smoke[3] == drink[3]) {
                                                                                                                            //13.德国人抽PRINCE牌香烟；
                                                                                                                            if (country[4] == smoke[4]) {
                                                                                                                                //15.抽BLENDS牌香烟的人和喝矿泉水的人相邻。
                                                                                                                                if (smoke[2] == (drink[4] + 1) || smoke[2] == (drink[4] - 1)) {

                                                                                                                                    for (pets[0] = 1; pets[0] <= 5; pets[0]++) {
                                                                                                                                        for (pets[1] = 1; pets[1] <= 5; pets[1]++) {
                                                                                                                                            for (pets[2] = 1; pets[2] <= 5; pets[2]++) {
                                                                                                                                                for (pets[3] = 1; pets[3] <= 5; pets[3]++) {
                                                                                                                                                    for (pets[4] = 1; pets[4] <= 5; pets[4]++) {
                                                                                                                                                        if (isRepeatNum(pets)) {
                                                                                                                                                            continue;
                                                                                                                                                        }
                                                                                                                                                        //2.瑞典人养狗；
                                                                                                                                                        if (country[1] == pets[0]) {
                                                                                                                                                            //6.抽PALL MALL牌香烟的人养鸟；
                                                                                                                                                            if (smoke[0] == pets[1]) {
                                                                                                                                                                //10.抽BLENDS香烟的人和养猫的人相邻；
                                                                                                                                                                if (smoke[2] == (pets[2] + 1) || smoke[2] == (pets[2] - 1)) {
                                                                                                                                                                    //11.养马的人和抽DUNHILL牌香烟的人相邻；
                                                                                                                                                                    if (pets[3] == (smoke[1] + 1) || pets[3] == (smoke[1] - 1)) {

                                                                                                                                                                        for (int i = 0; i < 5; i++) {
                                                                                                                                                                            if (country[i] == pets[4]) {
                                                                                                                                                                                if (i == 0) {
                                                                                                                                                                                    result += "英国人";
                                                                                                                                                                                } else if (i == 1) {
                                                                                                                                                                                    result += "瑞典人";
                                                                                                                                                                                } else if (i == 2) {
                                                                                                                                                                                    result += "丹麦人";
                                                                                                                                                                                } else if (i == 3) {
                                                                                                                                                                                    result += "挪威人";
                                                                                                                                                                                } else if (i == 4) {
                                                                                                                                                                                    result += "德国人";
                                                                                                                                                                                }
                                                                                                                                                                            }
                                                                                                                                                                        }
                                                                                                                                                                        result += "在养鱼";
                                                                                                                                                                        //System.out.println(result+"在养鱼");
                                                                                                                                                                    }
                                                                                                                                                                }
                                                                                                                                                            }
                                                                                                                                                        }
                                                                                                                                                    }
                                                                                                                                                }
                                                                                                                                            }
                                                                                                                                        }
                                                                                                                                    }
                                                                                                                                }
                                                                                                                            }
                                                                                                                        }
                                                                                                                    }
                                                                                                                }
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return result;
    }

    /**
     * 暴力破解法，无限for循环
     * 做到一半想到可以用二维数组= -、
     * 这个先写完，然后再写个二维数组的版本
     * 要修改顺序，加快迭代，不然真的太慢了= -、
     * 所以写两个版本，一个筛选，一个不筛选，这个就是不筛选的
     *
     * @return
     */
    public String baoliSingleNoCheck() {
        int[] country = {1, 1, 1, 1, 1};  //分别代表英国，瑞典，丹麦，挪威，德国
        int[] house = {1, 1, 1, 1, 1}; //红房子，白房子，绿房子，蓝房子，黄房子
        int[] drink = {1, 1, 1, 1, 1}; //喝茶,喝咖啡,喝牛奶,喝啤酒，喝矿泉水
        int[] smoke = {1, 1, 1, 1, 1}; //PALL MALL牌香烟,DUNHILL牌的烟,BLENDS香烟,BLUEMASTER牌香烟，PRINCE牌香烟
        int[] pets = {1, 1, 1, 1, 1}; //狗,鸟,猫,马,鱼
        String result = "";

        for (country[0] = 1; country[0] <= 5; country[0]++) {
            for (country[1] = 1; country[1] <= 5; country[1]++) {
                for (country[2] = 1; country[2] <= 5; country[2]++) {
                    for (country[3] = 1; country[3] <= 5; country[3]++) {
                        for (country[4] = 1; country[4] <= 5; country[4]++) {
                            if (isRepeatNum(country)) {
                                continue;
                            }

                            for (house[0] = 1; house[0] <= 5; house[0]++) {
                                for (house[1] = 1; house[1] <= 5; house[1]++) {
                                    for (house[2] = 1; house[2] <= 5; house[2]++) {
                                        for (house[3] = 1; house[3] <= 5; house[3]++) {
                                            for (house[4] = 1; house[4] <= 5; house[4]++) {
                                                if (isRepeatNum(house)) {
                                                    continue;
                                                }

                                                for (drink[0] = 1; drink[0] <= 5; drink[0]++) {
                                                    for (drink[1] = 1; drink[1] <= 5; drink[1]++) {
                                                        for (drink[2] = 1; drink[2] <= 5; drink[2]++) {
                                                            for (drink[3] = 1; drink[3] <= 5; drink[3]++) {
                                                                for (drink[4] = 1; drink[4] <= 5; drink[4]++) {
                                                                    if (isRepeatNum(drink)) {
                                                                        continue;
                                                                    }

                                                                    for (smoke[0] = 1; smoke[0] <= 5; smoke[0]++) {
                                                                        for (smoke[1] = 1; smoke[1] <= 5; smoke[1]++) {
                                                                            for (smoke[2] = 1; smoke[2] <= 5; smoke[2]++) {
                                                                                for (smoke[3] = 1; smoke[3] <= 5; smoke[3]++) {
                                                                                    for (smoke[4] = 1; smoke[4] <= 5; smoke[4]++) {
                                                                                        if (isRepeatNum(smoke)) {
                                                                                            continue;
                                                                                        }

                                                                                        for (pets[0] = 1; pets[0] <= 5; pets[0]++) {
                                                                                            for (pets[1] = 1; pets[1] <= 5; pets[1]++) {
                                                                                                for (pets[2] = 1; pets[2] <= 5; pets[2]++) {
                                                                                                    for (pets[3] = 1; pets[3] <= 5; pets[3]++) {
                                                                                                        for (pets[4] = 1; pets[4] <= 5; pets[4]++) {
                                                                                                            if (isRepeatNum(pets)) {
                                                                                                                continue;
                                                                                                            }

                                                                                                            //1.英国人住在红房子里；
                                                                                                            if (country[0] == house[0]) {
                                                                                                                //2.瑞典人养狗；
                                                                                                                if (country[1] == pets[0]) {
                                                                                                                    //3.丹麦人喝茶；
                                                                                                                    if (country[2] == drink[0]) {
                                                                                                                        //4.绿房子紧挨着白房子，在白房子的左边；
                                                                                                                        if (house[2] == (house[1] - 1)) {
                                                                                                                            //5.绿房子主人喝咖啡；
                                                                                                                            if (house[2] == drink[1]) {
                                                                                                                                //6.抽PALL MALL牌香烟的人养鸟；
                                                                                                                                if (smoke[0] == pets[1]) {
                                                                                                                                    //7.黄房子里的人抽DUNHILL牌的烟；
                                                                                                                                    if (house[4] == smoke[1]) {
                                                                                                                                        //8.住中间房子的人喝牛奶；
                                                                                                                                        if (drink[2] == 3) {
                                                                                                                                            //9.挪威人住在第一个房子里（最左边）；
                                                                                                                                            if (country[3] == 1) {
                                                                                                                                                //10.抽BLENDS香烟的人和养猫的人相邻；
                                                                                                                                                if (smoke[2] == (pets[2] + 1) || smoke[2] == (pets[2] - 1)) {
                                                                                                                                                    //11.养马的人和抽DUNHILL牌香烟的人相邻；
                                                                                                                                                    if (pets[3] == (smoke[1] + 1) || pets[3] == (smoke[1] - 1)) {
                                                                                                                                                        //12.抽BLUEMASTER牌香烟的人喝啤酒；
                                                                                                                                                        if (smoke[3] == drink[3]) {
                                                                                                                                                            //13.德国人抽PRINCE牌香烟；
                                                                                                                                                            if (country[4] == smoke[4]) {
                                                                                                                                                                //14.挪威人和住蓝房子的人相邻；
                                                                                                                                                                if (country[3] == (house[3] + 1) || country[3] == (house[3] - 1)) {
                                                                                                                                                                    //15.抽BLENDS牌香烟的人和喝矿泉水的人相邻。
                                                                                                                                                                    if (smoke[2] == (drink[4] + 1) || smoke[2] == (drink[4] - 1)) {

                                                                                                                                                                        for (int i = 0; i < 5; i++) {
                                                                                                                                                                            if (country[i] == pets[4]) {
                                                                                                                                                                                if (i == 0) {
                                                                                                                                                                                    result += "英国人";
                                                                                                                                                                                } else if (i == 1) {
                                                                                                                                                                                    result += "瑞典人";
                                                                                                                                                                                } else if (i == 2) {
                                                                                                                                                                                    result += "丹麦人";
                                                                                                                                                                                } else if (i == 3) {
                                                                                                                                                                                    result += "挪威人";
                                                                                                                                                                                } else if (i == 4) {
                                                                                                                                                                                    result += "德国人";
                                                                                                                                                                                }
                                                                                                                                                                            }
                                                                                                                                                                        }
                                                                                                                                                                        result += "在养鱼";
                                                                                                                                                                        //System.out.println(result+"在养鱼");
                                                                                                                                                                    }
                                                                                                                                                                }
                                                                                                                                                            }
                                                                                                                                                        }
                                                                                                                                                    }
                                                                                                                                                }
                                                                                                                                            }
                                                                                                                                        }
                                                                                                                                    }
                                                                                                                                }
                                                                                                                            }
                                                                                                                        }
                                                                                                                    }
                                                                                                                }
                                                                                                            }
                                                                                                        }
                                                                                                    }
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return result;
    }


    /**
     * 判断数组是否包含重复元素
     * 用常规的判断太头疼了
     * 所以写了这么一个双层循环
     *
     * @param a
     * @return 布尔值
     */
    static boolean isRepeatNum(int a[]) {
        for (int i = 0; i < 4; i++) {
            for (int j = i + 1; j < 5; j++) {
                if (a[i] == a[j]) {
                    return true;
                }
            }
        }
        return false;
    }

}

